<?
include('../variables.php');
$save_type="MySQL";

if($_SERVER['QUERY_STRING']){
        $params = decode_params();
        if($params){
                while(list($key,$value) = each($params)){
                        $$key  = $value;
                }

        }
        $params = "";
}

$dbh = get_master_connect();
if (!$dbh) {
	 			$errorMsg = translate('cannot_process_request');
				include_once('errorMsg.html');	
//      echo "We are sorry but we cannot process your request at the moment<BR>";
        exit_now();
}
	
for($id=0;$id<18000;$id++){
	$plans =Array();
	$product_off=Array();
	$products = Array();
	if(!$plans= get_plan_settings($id)){
		echo "error id = $id\n";
		continue;
	}
	if( $plans['Business'][has_domain] =='true'){
		$has_domain='on';
		$has_domain_inplan ='';
	}else if( $plans['Business'][has_domain] =='inplan'){
		$has_domain='on';
		$has_domain_inplan ='on';

	}else {
		$has_domain='';
		$has_domain_inplan ='';

	}		

	$db['1']['pass'] = $pg_password; //"w9JwRoA";
	$db['1']['user'] = $pg_username; //"root";
	$db['1']['name'] = $pg_db; //"rp2";
	$db['1']['host'] = $pg_host; //"209.25.134.5";

	if(!$dbh2)
		$dbh2 = pg_connect("dbname=".$db[1][name]." host=".$db[1][host]." user=".$db[1][user]." password=".$db[1][pass]."");
	
	//$id
	$qq = "select product_id,type,name from products where reseller_id='$id'";
	if($result = pg_query($dbh2,$qq)){
		while($row = pg_fetch_assoc($result)){
			$products[] = $row;
		}
	}else return 0;

	if(!is_array($products)){
		echo "error no products for : $id\n";
		continue;
	}
	$product_off=Array();
	if($has_domain){
		foreach($products as $p){
			if($p[type]=='domain'){
				$product_off[domains_offering][ $p[product_id]] = $has_domain;
			}
		}
	}
	reset($products);
	foreach($products as $p){
		if($p[type]=='plan'){
			foreach($plans as $plan){
				if(strtolower($p[name]) == strtolower($plan[plan])){
					if($plan[offering]){
						$product_off[plans_offering][$p[product_id]] = $plan[offering];
						$product_off[plans_on_index][$p[product_id]] = $plan[display_on_index];
						if($has_domain_inplan)
							$product_off[domains_free][$p[product_id]][12] = $has_domain_inplan;
						$product_off[periods][$p[product_id]][12] = $plan[offering];
					}
					
					
				}
			}
		}else 	if($p[type]=='service'){
			$product_off[services_offering][$p[product_id]] = 'on';
		}
	}
	$qq= "update stores set products='".base64_encode(serialize($product_off))."' where reseller_id='$id';\n";
	$result = pg_query($dbh2,$qq);
	//`echo "$qq">>duplicate_queries.txt`;
}
		
?>
